package com.ruoyi.system.mapper;

import com.ruoyi.system.domain.StorySysEmployee;
import com.ruoyi.system.domain.SysEmployee;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface StorySysEmployeeMapper {

    /**
     * 插入
     * @param storySysEmployee
     * @return
     */
    @Insert("insert into story_sysemployee(userId,storyId,classId,departmentId,departmentName,sysEmployeeId,sysEmployeeName,opinion,opinionTime,transferNumber) values(#{userId},#{storyId},#{classId},#{departmentId},#{departmentName},#{sysEmployeeId},#{sysEmployeeName},#{opinion},#{opinionTime},#{transferNumber})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")/*增加这个注解插入记录后会返回自增长的id*/
    public int insertStoryAndSysEmployee(StorySysEmployee storySysEmployee);


    @Select("select departmentName, sysEmployeeName, opinion, opinionTime, operationTime from story_sysemployee where storyId = #{storyId} and transferNumber = #{transferNumber}")
    public List<StorySysEmployee> selectNameAndOpinionById(@Param("storyId") String storyId, @Param("transferNumber") int transferNumber);


    /**
     *
     * @param storyId
     * @return
     */
    @Select("select count(*) from story_sysemployee where storyId = #{storyId} and transferNumber = #{transferNumber}")
    public int selectTransferNumberByStoryId(@Param("storyId") int storyId, @Param("transferNumber") int transferNumber);

    /**
     * 根据id和移交次数查询回复信息
     * @param storyId
     * @param transferNumber
     * @return
     */
    @Select("select id, reply, replyTime, departmentName, sysEmployeeName, isBack, operationTime from story_sysemployee where storyId = #{storyId} and transferNumber = #{transferNumber}")
    public List<StorySysEmployee> selectReply(@Param("storyId") int storyId, @Param("transferNumber") int transferNumber);

    @Update("update story_sysemployee set isBack = 1 where storyId = #{ storyId } and transferNumber = #{transferNumber}")
    public int updateBack(@Param("storyId") int storyId, @Param("transferNumber") int transferNumber);

    /**
     * 查询第一次移交的审核意见
     * @param storyId
     * @return
     */
    @Select("select opinion from story_sysemployee where storyId = #{storyId} and transferNumber = 1")
    String selectOpinion(@Param("storyId") int storyId);

    /**
     * 更新移交信息的状态为已完成状态2
     * @param replyId
     * @return
     */
    @Update("update story_sysemployee set isBack = #{ isBack } where id = #{ replyId }")
    int updateReplyStateByCompletion(@Param("replyId") int replyId, @Param("isBack") int isBack);

    /**
     * 根据小事id查询出移交信息的状态
     * @param storyId
     * @return
     */
    @Select("select id, isBack from story_sysemployee where storyId = #{storyId} and transferNumber = #{transferNumber}")
    List<StorySysEmployee> selectIsBackByStoryIdAndTransferNumber(@Param("storyId") int storyId , @Param("transferNumber") int transferNumber);

    /**
     * 根据id更新时间
     * @return
     */
    @Update("update story_sysemployee set operationTime = #{ operationTime } where id = #{ replyId }")
    int updateOperationTime(@Param("replyId") int replyId, @Param("operationTime") String operationTime);



    /**
     * 查询为回复的移交信息
     * @return
     */
    @Select("select sysEmployeeId, opinionTime from story_sysemployee where reply is null")
    List<StorySysEmployee> selectListByOpinionIsNull();

    /**
     * 根据storyId 查询移交的小事是否被答复
     * @return
     */
    @Select("select sysEmployeeId from story_sysemployee where storyId = #{ storyId } and transferNumber = #{ transferNumber } and isBack = -1")
    List<String> selectEmpLastOne(@Param("storyId") int storyId, @Param("transferNumber") int transferNumber);


    @Select("select opinion, reply, departmentName, sysEmployeeName, transferNumber from story_sysemployee where storyId = #{storyId}")
    List<StorySysEmployee> selectEmpsByStorytId(@Param("storyId") int storyId);
}
